package yc.day0109.travelt.dao.impl;



import yc.day0109.lottery.utils.BaseDao;
import yc.day0109.travelt.dao.TourRouteDao;
import yc.day0109.travelt.po.TourRoute;

import java.sql.SQLException;
import java.util.ArrayList;

import java.util.List;
import java.util.Map;

public class TourRouteDaoImpl extends BaseDao implements TourRouteDao {
    @Override
    public List<TourRoute> queryTourRoute(Map<String, Object> params) {
        List<TourRoute> tourRouteList = new ArrayList<>();
        TourRoute tourRoute = null;
        String sql = "select tr_name,tr_type,tr_price,tr_phone,tr_user,to_char(create_time, 'yyyy-MM-dd')create_time from tour_route";
        super.rs = super.executeQuery(sql);
        try{
            while(rs.next()){
                tourRoute = new TourRoute();
                tourRoute.setTrname(rs.getString("tr_name"));
                tourRoute.setTrType(rs.getInt("tr_type"));
                tourRoute.setTrPrice(rs.getInt("tr_price"));
                tourRoute.setTrPhone(rs.getString("tr_phone"));
                tourRoute.setTrUser(rs.getString("tr_user"));
                tourRoute.setCreateTime(rs.getString("create_time"));
                tourRouteList.add(tourRoute);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(rs,pstmt,conn);
        }
        return tourRouteList;
    }

    @Override
    public int addTourRoute(TourRoute tourRoute) {
        int addRes = 0;
        StringBuffer sql = new StringBuffer();
        sql.append(" insert into tour_route ");
        sql.append(" (tr_id,tr_name,tr_type,tr_price,tr_phone,tr_user,create_time) ");
        sql.append(" values(tr_id.nextval,?,?,?,?,?,to_date(?, 'yyyy-mm-dd')) ");
        addRes = super.executeUpdate(sql.toString(),new Object[]{tourRoute.getTrname(),tourRoute.getTrType(),tourRoute.getTrPrice(),
        tourRoute.getTrPhone(),tourRoute.getTrUser(),tourRoute.getCreateTime()});
        return addRes;
    }
    public int deleteTourRoute(String trname){
        StringBuffer sql = new StringBuffer();
        sql.append(" delete from tour_route where tr_name = ? ");
        return super.executeUpdate(sql.toString(),new Object[]{trname});
    }
}
